package leeon.bbsbrower.util;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import leeon.bbsbrower.actions.Config;

public class HTMLUtil {

	/**
	 * 寻找字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @param strLength 目标的长度
	 * @return
	 */
	public static String findStr(String source, String startTag, int strLength) {
		return findStr(source, startTag, strLength, 0);
	}	
	
	/**
	 * 寻找字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @param strLength 目标的长度
	 * @param startFrom 开始标志后第几位开始截取
	 * @return
	 */
	public static String findStr(String source, String startTag, int strLength, int startFrom) {
		int s = source.indexOf(startTag);
		if (s == -1) return null;
		else s = s + startTag.length() + startFrom;
		
		int e = s + strLength;
		
		if (e >= source.length()) return source.substring(s);
		else return source.substring(s, s+strLength);
	}
	
	
	/**
	 * 寻找字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @return
	 */
	public static String findStr(String source, String startTag) {
		return findStr(source, startTag, null, 0);
	}
	
	/**
	 * 寻找字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @param endTag 结束标志
	 * @return
	 */
	public static String findStr(String source, String startTag, String endTag) {
		return findStr(source, startTag, endTag, 0);
	}
	
	/**
	 * 寻找字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @param endTag 结束标志
	 * @param startFrom 开始标志后第几位开始截取
	 * @return
	 */
	public static String findStr(String source, String startTag, String endTag, int startFrom) {
		int s = source.indexOf(startTag);
		if (s == -1) return null;
		else s = s + startTag.length() + startFrom;
		
		int e = -1;
		if (endTag != null) e = source.indexOf(endTag, s);
		
		if (e == -1) return source.substring(s);
		else return source.substring(s, e);
	}
	
	
	/**
	 * 寻找多个字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @param endTag 结束标志
	 * @return
	 */
	public static String[] findStrs(String source, String startTag, String endTag) {
		return findStrs(source, startTag, endTag, 0);
	}
	
	
	/**
	 * 寻找多个字符
	 * @param source 原串
	 * @param startTag 开始标志
	 * @param endTag 结束标志
	 * @param startFrom 开始标志后第几位开始截取
	 * @return
	 */
	public static String[] findStrs(String source, String startTag, String endTag, int startFrom) {
		int s = 0, e = 0;
		ArrayList<String> list = new ArrayList<String>();
		while ((s = source.indexOf(startTag, e)) != -1) {
			s = s + startTag.length() + startFrom;
			e = source.indexOf(endTag, s);
			
			if (e == -1) list.add(source.substring(s));
			else list.add(source.substring(s, e));
		}
		return list.toArray(new String[0]);
	}
	
	
	/**
	 * 去掉文章中的font和a标记，还原成完整的html source
	 * @param source
	 * @return
	 */
	public static String removeHtmlTag(String source) {
		if (source != null) {
			source = source.replaceAll("<input[^>]*>", "");
			source = source.replaceAll("<font[^>]*>", "");
			source = source.replaceAll("</font>", "");
			source = source.replaceAll("<a[^>]*>", "");
			source = source.replaceAll("</a>", "");
			source = source.replaceAll("<b>", "");
			source = source.replaceAll("</b>", "");
			source = source.replaceAll("<span>", "");
			source = source.replaceAll("</span>", "");
			return source;
		} else 
			return null;
	}
	
	/**
	 * 处理文章中的图片连接
	 * @param source
	 * @return
	 */
	public static String dealImageTag(String source) {
		if (source != null) {
			source = source.replaceAll("on\\w*='[^']*'", "#");
			source = source.replaceAll("<IMG border=0 SRC='", Config.SERVER_URL);			
			source = source.replaceAll("<IMG SRC='", Config.SERVER_URL);
			source = source.replaceAll("' #>", "");
			source = source.replaceAll("' # # # border=0>", "");
			return source;
		} else
			return null;
	}
	
	/**
	 * 处理用户的经验值
	 * 0 -
	 * 1 =
	 * 2 +
	 * 3 *
	 * 4 #
	 * 5 A
	 * @param source
	 * @return
	 */
	public static String dealUserLevel(String source) {
		if (source != null) {
			source = source.replaceAll("<img src=/images/level/0.gif border=0 align=absmiddle>", "-");
			source = source.replaceAll("<img src=/images/level/1.gif border=0 align=absmiddle>", "=");
			source = source.replaceAll("<img src=/images/level/2.gif border=0 align=absmiddle>", "+");
			source = source.replaceAll("<img src=/images/level/3.gif border=0 align=absmiddle>", "*");
			source = source.replaceAll("<img src=/images/level/4.gif border=0 align=absmiddle>", "#");
			source = source.replaceAll("<img src=/images/level/5.gif border=0 align=absmiddle>", "A");
			
			return source;
		} else
			return null;
	}
	
	/**
	 * 根据html语法，从table里抽取一行的信息
	 * @param source
	 * @return
	 */
	public static String[] dealTableRows(String source) {
		ArrayList<String> list = new ArrayList<String>();
		if (source != null) {
			int s = 0, e = 0;
			while ((s = source.indexOf("<tr", e)) != -1) {
				//查找<tr>的结束
				s = source.indexOf(">", s);
				if (s == -1) break;
				else s++;
				
				//查找下一个<tr>
				e = source.indexOf("<tr", s);
				//没有了，是最后一行
				if (e == -1) {
					e = source.length();
					if (source.charAt(e-1) == '>' && source.charAt(e-2) == 'r' &&
						source.charAt(e-3) == 't' && source.charAt(e-4) == '/' &&
						source.charAt(e-5) == '<') {						
						e = e - 5;						
					}					
				}
				//有，那么判断前面有没有</tr>
				else {
					if (source.charAt(e-1) == '>' && source.charAt(e-2) == 'r' &&
						source.charAt(e-3) == 't' && source.charAt(e-4) == '/' &&
						source.charAt(e-5) == '<') {						
						e = e - 5;						
					}
				}
				list.add(source.substring(s, e));
			}
		} 
		return list.toArray(new String[0]);
	}
	
	/**
	 * 根据html语法，从一行中抽取一个单元格的信息
	 * @param source
	 * @return
	 */
	public static String[] dealTableCells(String source) {
		ArrayList<String> list = new ArrayList<String>();
		if (source != null) {
			int s = 0, e = 0;
			while ((s = source.indexOf("<td", e)) != -1) {
				//查找<tr>的结束
				s = source.indexOf(">", s);
				if (s == -1) break;
				else s++;
				
				//查找下一个<tr>
				e = source.indexOf("<td", s);
				//没有了，是最后一行
				if (e == -1) {
					e = source.length();
					if (source.charAt(e-1) == '>' && source.charAt(e-2) == 'd' &&
						source.charAt(e-3) == 't' && source.charAt(e-4) == '/' &&
						source.charAt(e-5) == '<') {
						e = e - 5;						
					}					
				}
				//有，那么判断前面有没有</td>
				else {
					if (source.charAt(e-1) == '>' && source.charAt(e-2) == 'd' &&
						source.charAt(e-3) == 't' && source.charAt(e-4) == '/' &&
						source.charAt(e-5) == '<') {						
						e = e - 5;						
					}
				}
				list.add(source.substring(s, e));
			}
		}
		return list.toArray(new String[0]);
	}	
	
	
	public static void main(String[] s) {
		String text = "xxxxx. http://bbs.fudan.edu.cn/upload/PIC/1201065382-7002.JPG xxxxxxxxeeeeeefgga  http://bbs.fudan.edu.cn/upload/PIC/xxxxxxx.GIF xxxxxx";
		Pattern p = Pattern.compile("http://[\\.\\w-/]*\\.(JPG|GIF|BMP)");
		Matcher m = p.matcher(text);
		while (m.find()) {
			System.out.println(m.group());
		}
	}

}
